/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.display;

import plash.events.LoadEvent;
import plash.geom.Rectangle;
import processing.core.PApplet;
import processing.core.PImage;

/**
 *
 * @author #0K Srinivasan <hashok.com>
 */
public class Loader extends InteractiveObject
{
    private String _imageURL;
    private PImage _image;
    private boolean _imageLoadingStarted;
    private boolean _imageLoadingFinished;
    
    public Loader()
    {
        super();
    }
    
    public Loader(String imagePath)
    {
        super();
        _imageURL = imagePath.trim();
    }
    
    @Override
    protected void _updateBoundingBox()
    {
        if(_image == null)
        {
            __width = __height = 0;
        }
        else
        {
            __width = _image.width;
            __height = _image.height;
        }
        
        __boundingBox = new Rectangle(0,0,__width,__height);

        if (this.__parent != null)
        {
            __boundingBox.setX((int) (__boundingBox.getX() * this.getParent().getScaleX()));
            __boundingBox.setY((int) (__boundingBox.getY() * this.getParent().getScaleY()));
        }
        
        __boundingBox.setX(this.getAbsoluteX() + __boundingBox.getX());
        __boundingBox.setY(this.getAbsoluteY() + __boundingBox.getY());
        
        __boundingBox.setWidth((int) (this.getAbsoluteScaleX() * __boundingBox.getWidth()));
        __boundingBox.setHeight((int) (this.getAbsoluteScaleY() * __boundingBox.getHeight()));

        if (this.__rotation != 0)
        {
            Rectangle newBounds = __findRotatedBounds(__boundingBox);
            __boundingBox = newBounds;
        }
    }
    
    private void _loadImage()
    {
        if(this.getStage() == null)
        {
            return;
        }
        if(_imageURL == null)
        {
            return;
        }
        if(_imageURL.isEmpty())
        {
            return;
        }
        
        if(_image != null)
        {
            _image = null;
            System.gc();
        }
//        _image = this.getStage().loadImage(_imageURL);
        _imageLoadingStarted = true;
        _imageLoadingFinished = false;
        _image = this.getStage().requestImage(_imageURL);
    }
    
    @Override
    public void draw()
    {
        if(!_imageLoadingFinished)
        {
            if(_imageLoadingStarted)
            {
                if(_image.width == 0)
                {
                    dispatchEvent(new LoadEvent(LoadEvent.IMAGE_LOAD_START));
                }
                else
                {
                    _imageLoadingFinished = true;
                    dispatchEvent(new LoadEvent(LoadEvent.COMPLETE));
                }
            }
        }
        if (!__visible)
        {
            return;
        }

        if (__stage == null)
        {
            return;
        }

        if (this.__parent == null)
        {
            if (!this.__name.equals("_root"))
            {
                return;
            }
        }

        if (this.__painter == null)
        {
            return;
        }
        
        if(_image == null)
        {
            return;
        }

        if (this.__hasMasker || this.__isMasker)
        {
            this.__painter.beginDraw();
        }

        //Then proceed to the painting of the object on the screen

        this.__painter.pushMatrix();

        this.__painter.translate(this.__x, this.__y);

        this.__painter.rotate(this.__radiansRotation);
        this.__painter.scale(__scaleX, __scaleY);

        if (this.__hasMasker || this.__isMasker)
        {
            this.__painter.background(0, 0, 0, 0);
        }       

        this._updateBoundingBox();
        
        this.__painter.image(_image, 0, 0);

        this.__painter.popMatrix();

        super.draw();
    }

    public String getImageURL()
    {
        return _imageURL;
    }

    public void setImageURL(String url)
    {
        this._imageURL = url;
        _loadImage();
    }    
    
    @Override
    public void setStage(IStage stage)
    {
        super.setStage(stage);
        _loadImage();
    }
}
